#include "knobskin.h"
#include <allegro5/allegro_image.h>
#include <iostream>
#include <cstdlib>
using namespace std;

KnobSkin::KnobSkin(const string &imagefile, int size, int cols)
: m_size(size), m_pBitmap(NULL), m_cols(cols) {
     m_pBitmap = al_load_png(imagefile.c_str());
     if (!m_pBitmap) {
	  cout << "error loading knob image '" << imagefile <<  "'" << endl;
	  exit(1);
     }
}

KnobSkin::~KnobSkin() {
     al_destroy_bitmap(m_pBitmap);
}

int KnobSkin::size() const {
     return m_size;
}

ALLEGRO_BITMAP* KnobSkin::bitmap() const {
     return m_pBitmap;
}

Rect KnobSkin::rect(double val) const {
     // get image number
     int i = val * (DegreeCount - KnobSkin::Offset * 2 - 1);
     int r = i / m_cols;
     int c = i % m_cols;
     return Rect(c * m_size, r * m_size, m_size, m_size);
}
